<template>
  <div style="height: 1180px;position: absolute;left: 400px">
    <el-card style="z-index: -1;width: 600px;margin: 0 auto;height: 1280px;background-image: linear-gradient(to bottom, #00c9a7, #7bd6a6, #b2e3af, #ddf0c3, #fefedf);">
      <div>
        <span style="font-size: 25px;color: #000;position: relative;left: 0px;top: 50px">学习计划</span>
        <el-button style="position:relative;right: -370px;top: 45px;" color="#0089BA" @click="dialogVisible=true" >新增考试</el-button>
      </div>
      <p style="text-align: center;position: relative;top: 100px;font-size: 25px">加油,快完成了</p>
      <!--新增考试弹窗 -->
      <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width: 600px;padding: 40px" :before-close="handleClose">
        <el-row :gutter="40">
          <el-col :span="12">
            <el-form-item>
              <span>考试名</span>
              <el-input placeholder="请输入考试名" v-model="plans.examName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <span>选择考试时间</span>
              <el-select v-model="plans.examDate" >
                <el-option value="上半年">上半年</el-option>
                <el-option value="下半年">下半年</el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <template #footer>
          <el-button style="float: right" color="#0089BA" @click="getData">保存</el-button>
          <el-button style="float: right;position: relative;right: 5px" color="#0089BA">修改</el-button>
        </template>
      </el-dialog>

      <p style="position: relative;text-align: center;top: 210px;z-index: 10;font-size: 30px;">倒计时</p>
      <p style="text-align: center;position: relative;top: 190px;z-index: 10;font-size: 110px">{{days}}</p>
      <el-card style="width: 337px;height: 430px;position: relative;left: 115px;top: -250px;z-index: 1">
        <img src="/img/wjf/1.jpeg" style="height: 450px;position: relative;top: -20px;left: -20px;z-index: 2;filter: opacity(70%)">
      </el-card>
      <hr style="position: relative;top: -200px">
      <div style="position: relative;top: -150px">
        <span style="font-size: 25px">学习计划</span>
        <el-button style="position: relative;left: 370px;top: -5px" color="#0089BA" @click="dialogAddVisible=true">新增计划</el-button>

        <!--新增计划弹窗 -->
        <el-dialog :title="dialogTitle" v-model="dialogAddVisible" style="width: 600px;padding: 40px" :before-close="handleClose">
          <el-row :gutter="40" >
            <el-col :span="12">
              <el-form label-position="top">
                <el-form-item label="计划名">
                  <el-input placeholder="请输入计划名称" v-model="addPlan.planName"></el-input>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="12">
              <el-form label-position="top">
                <el-form-item label="考试科目">
                  <el-input placeholder="请输入考试科目" v-model="addPlan.subjectName"></el-input>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
          <el-row :gutter="40" style="position: relative;top: 40px">
            <el-col :span="12">
              <el-form-item label="时间">
                <el-date-picker
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="YYYY-MM-DD HH:mm:ss"
                    format="YYYY-MM-DD HH:mm:ss"
                    v-model="times"
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form style="float: right">
            <el-form-item>
              <el-button color="#0089BA" @click="update" >修改</el-button>
              <el-button color="#0089BA" @click="add" >添加</el-button>
            </el-form-item>
          </el-form>
        </el-dialog>

        <!-- 显示计划信息表格 -->
        <el-table style="top: 70px" :data="tableData">
          <el-table-column label="编号" type="index" prop="id" align="center" width="80"></el-table-column>
          <el-table-column label="考试科目" prop="subjectName" align="center"></el-table-column>
          <el-table-column label="剩余天数" prop="days" align="center"></el-table-column>
          <el-table-column label="今日状态" prop="planStatus" align="center"
          :formatter="planStatusFormatter"></el-table-column>
          <el-table-column label="操作">
            <template #default="scope">
              <el-button link type="primary" @click="auditPlan(scope.row.id)">编辑</el-button>
              <el-button link type="primary" @click="deletePlan(scope.row.id)">移除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-popover
            placement="top-start"
            title="..."
            :width="200"
            trigger="hover"
            content="累计连续签到5天可得一枚勋章"
        >
          <template #reference>
            <el-button style="position: relative;top: 130px;left: 250px" color="#0089BA" @click="clock">打卡</el-button>
          </template>
        </el-popover>

        <span style="position: relative;top: 190px;left: 150px">你已累计签到:</span>
        <hr style="position:relative; top: 200px">
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage} from "element-plus";

const dialogVisible = ref(false);
const dialogTitle = ref('新增考试');
const dialogAddVisible = ref(false);

const handleClose = () => {
  if (confirm('确定要关闭吗?')) {
    dialogAddVisible.value = false;
    dialogVisible.value = false;
    addPlan.value = {};
    times.value = {};
  }
}
onMounted(()=>{
  loadPlan();
})
//获取当前登录用户
const user = ref(getUser());
//定义变量保存起止时间
const times = ref([]);
//定义变量保存考试信息
const addPlan = ref({
  planName: '',
  subjectName:'',
  startTime:'',
  endTime:''
})

//定义方法新增计划
const add = ()=>{
  addPlan.value.startTime=times.value[0];
  addPlan.value.endTime=times.value[1];
  let date = new Date();
  addPlan.value.userId = user.value.id;
  if (times.value[0] <= date){
    let date1 = new Date(date);
    let date2 = new Date(times.value[1]);
    let time1 = date1.getTime();
    let time2 = date2.getTime();
    let time = time2 - time1;
    let day = time /1000/60/60/24;
    let days= Math.abs(day);
    addPlan.value.days = days;
  }else {
    let date1 = new Date(times.value[0]);
    let date2 = new Date(times.value[1]);
    let time1 = date1.getTime();
    let time2 = date2.getTime();
    let time = time2 - time1;
    let day = time /1000/60/60/24;
    let days= Math.abs(day);
    addPlan.value.days = days;
  }
  dialogTitle.value='新增计划'
  console.log(addPlan.value);
  let data = qs.stringify(addPlan.value)
  axios.post(BASE_URL+'/v1/plan/addPlan',data).then((response)=>{
    if (response.data.code == 2000){
      ElMessage.success("添加成功");
      loadPlan();
    }else {
      ElMessage.error(response.data.msg)
    }
  })
}
const tableData = ref([]);

//定义方法加载数据
const loadPlan = ()=>{
  let userId = user.value.id
  console.log(userId)
  axios.get(BASE_URL+'/v1/plan/select?userId='+userId).then((response)=>{
    if (response.data.code==2000){
      tableData.value = response.data.data;
      console.log(tableData.value)
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
var id1
//定义方法进行编辑
const auditPlan = (id)=>{
    id1=id;
    dialogAddVisible.value=true;
    dialogTitle.value='编辑计划';
    axios.get(BASE_URL+'/v1/plan/select?id='+id).then((response)=>{
      if (response.data.code==2000){
        //如果请求成功返回数据的第一条
        addPlan.value = response.data.data[0];
        console.log(addPlan.value)
        times.value[0] = addPlan.value.startTime;
        times.value[1] = addPlan.value.endTime;
        console.log(times.value)
      }
    })
}

const update = ()=>{
  addPlan.value.id=id1;
  let data = qs.stringify(addPlan.value)
  axios.post(BASE_URL+'/v1/plan/update',data).then((response)=>{
    if (response.data.code == 2000){
      ElMessage.success('修改成功');
      loadPlan();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义方法删除计划
const deletePlan = (id)=>{
  axios.get(BASE_URL+'/v1/plan/delete/'+id).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('删除成功')
      loadPlan();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}

//定义方法进行打卡
const clock = ()=>{
  let userId = user.value.id
  axios.post(BASE_URL+'/v1/plan/updateStatus/'+userId).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('打卡成功')
      loadPlan();
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
const planStatusFormatter = (row,column,cellvalue,index)=>{
  if (cellvalue==0){
    cellvalue = '未打卡'
  }else if (cellvalue==1){
    cellvalue = '已打卡'
  }
  return cellvalue;
}

</script>
<script>
import axios from "axios";
import {ref} from "vue";
import qs from "qs";
import {ElMessage} from "element-plus";

const plans = ref({
  examName: '',
  status:'',
  examDate:''
})

//定义方法保存考试
const getDate = () => {
  let data = qs.stringify(plans.value);
  axios.get(BASE_URL + '/v1/plan/getDate?'+data).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('保存成功');
      plans.value = response.data.data;
      console.log(plans.value)
    } else {
      ElMessage.error(response.data.msg);
    }
  })
}

let date;
const getData = ()=>{
  date = "2024-12-28"
}

export default {
  name: 'Countdown',
  //定义方法获取该考试的日期

  data() {
    return {
      days: 0
    }
  },
  mounted() {
    setInterval(() => {
      const currentDate = new Date();
      const targetDate = new Date(date);
      const diffTime = targetDate - currentDate;
      const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24));
      this.days = diffDays;
    }, 1000);
  }
}
</script>
